//package com.ecjtu.stadiumre_servations.service.impl;
//
//import com.baomidou.mybatisplus.core.toolkit.Wrappers;
//import com.ecjtu.stadiumre_servations.config.RabbitMQConfig;
//import com.ecjtu.stadiumre_servations.entity.domain.Venue;
//import com.ecjtu.stadiumre_servations.mapper.VenueMapper;
//import org.springframework.amqp.rabbit.annotation.RabbitListener;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.EnableAsync;
//import org.springframework.scheduling.annotation.EnableScheduling;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Service;
//
//import java.time.LocalDateTime;
//import java.util.Map;
//import java.util.concurrent.Executors;
//import java.util.concurrent.ScheduledExecutorService;
//import java.util.concurrent.TimeUnit;
//
//@Service
//@EnableAsync
//@EnableScheduling
//public class VenueStatusUpdateConsumer {
//    @Autowired
//    private VenueMapper venueMapper;
//    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(10);
//
//    @RabbitListener(queues = RabbitMQConfig.QUEUE_NAME)
//    public void handleVenueStatusUpdate(Map<String, Object> message) {
//        String venueId = (String) message.get("venueId");
//        LocalDateTime startTime = (LocalDateTime) message.get("startTime");
//        LocalDateTime endTime = (LocalDateTime) message.get("endTime");
//
//        LocalDateTime now = LocalDateTime.now();
//        if (startTime.isBefore(now)) {
//            // 如果开始时间已经过去，立即将场馆状态设为已被预约
//            updateVenueStatus(venueId, "已被预约");
//        } else {
//            // 否则，在开始时间到达时将场馆状态设为已被预约
//            long delay = java.time.Duration.between(now, startTime).toMillis();
//            scheduler.schedule(() -> updateVenueStatus(venueId, "已被预约"), delay, TimeUnit.MILLISECONDS);
//        }
//
//        // 在结束时间到达时将场馆状态设为可预约
//        long endDelay = java.time.Duration.between(now, endTime).toMillis();
//        scheduler.schedule(() -> updateVenueStatus(venueId, "可预约"), endDelay, TimeUnit.MILLISECONDS);
//    }
//
//    private void updateVenueStatus(String venueId, String status) {
//        Venue venue = venueMapper.selectOne(
//                Wrappers.<Venue>lambdaQuery().eq(Venue::getVenueId, venueId)
//        );
//        if (venue != null) {
//            venue.setVenueStatus(status);
//            venueMapper.updateById(venue);
//        }
//    }
//
////    @Scheduled(fixedRate = 60000) // 每分钟检查一次过期预约
////    public void checkExpiredBookings() {
////        LocalDateTime now = LocalDateTime.now();
////        // 这里可以添加查询过期预约的逻辑，并更新场馆状态
////    }
//}